<?php
namespace App\Transformers;

use League\Fractal\TransformerAbstract;
use App\Model\Bistro;
use App\Model\BistroIncomeDay;
use App\Model\BistroIncomeMonth;

class BistroTransformer extends TransformerAbstract{

    public function transform(Bistro $item){
        return [
            'id' => $item->id,
            'head_store_id' => $item->head_store_id,
            'name' => $item->name,
            'qr_code' => $item->qr_code,
            'start_fee' => $item->start_fee,
            'address' => $item->address,
            'area' => $item->area,
            'account' => $item->account,
            'created_at' =>$item->created_at->toDateTimeString(),
            'updated_at' =>$item->updated_at->toDateTimeString()
        ];
    }

    public static function forBackendStatis() {
        return function (Bistro $item){
            return [
                'id' => $item->id,
                'head_store_id' => $item->head_store_id,
                'name' => $item->name,
                'qr_code' => $item->qr_code,
                'start_fee' => $item->start_fee,
                'address' => $item->address,
                'area' => $item->area,
                'account' => $item->account,
                'today' => BistroTransformer::forStaticToday($item),
                'this_month' => BistroTransformer::forStaticThisMonth($item)
            ];
        };
    }

    private static function forStaticToday($item) {//今天
        $date = date('Y-m-d');
        $income = BistroIncomeDay::where('bistro_id', $item->id)->where('date', $date)->sum('income');
        $outcome = BistroIncomeDay::where('bistro_id', $item->id)->where('date', $date)->sum('outcome');
        return [
            'income' => $income,
            'outcome' => $outcome
        ];
    }

    private static function forStaticThisMonth($item) {//当月
        $month = date('Y-m');
        $income = BistroIncomeMonth::where('bistro_id', $item->id)->where('month', $month)->sum('income');
        $outcome = BistroIncomeMonth::where('bistro_id', $item->id)->where('month', $month)->sum('outcome');
        return [
            'income' => $income,
            'outcome' => $outcome
        ];
    }
}